package top.huhuiyu.springboot.study.listener;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;
import top.huhuiyu.springboot.study.mapper.UtilMapper;
import top.huhuiyu.springboot.study.util.IpUtils;

/**
 * @author 胡辉煜
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class AppListener implements ApplicationListener<ApplicationReadyEvent> {
  private final Environment environment;
  private final UtilMapper utilMapper;

  @Override
  public void onApplicationEvent(ApplicationReadyEvent event) {
    log.debug("应用程序启动完毕");
    // 获取应用程序的端口信息和本机ip
    Integer port = environment.getProperty("server.port", Integer.class);
    String ip = IpUtils.getIpAddress();
    log.info("http://{}:{}", ip, port);
    log.info("http://{}:{}", "127.0.0.1", port);
    log.info("数据库当前时间：{}", utilMapper.queryNow());
  }
}
